home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gsview10.zip / print.c < prev    next >
C/C++ Source or Header  |  1993-07-29  |  24KB  |  824 lines

  1. /*
  2.  * print.c --   Printing operations for GSVIEW.EXE, 
  3.  *              a graphical interface for MS-Windows Ghostscript
  4.  * Copyright (C) 1993  Russell Lang
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  *   Author: Russell Lang
  21.  * Internet: rjl@monu1.cc.monash.edu.au
  22.  */
  23.  
  24. #define STRICT
  25. #include <windows.h>
  26. #include <windowsx.h>
  27. #include <commdlg.h>
  28. #include <shellapi.h>
  29. #include <mmsystem.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <ctype.h>
  34. #include <dir.h>
  35. #include <io.h>
  36. #define NeedFunctionPrototypes 1
  37. #include "ps.h"
  38. #include "gsview.h"
  39.  
  40. static char pcfname[MAXSTR];    /* name of temporary command file for printing */
  41. static char pfname[MAXSTR];    /* name of temporary file for printing options */
  42. int gp_printfile(char *filename, char *port);
  43.  
  44. /* documented in Device Driver Adaptation Guide */
  45. /* Prototypes taken from print.h */
  46. DECLARE_HANDLE(HPJOB);
  47.  
  48. HPJOB   WINAPI OpenJob(LPSTR, LPSTR, HPJOB);
  49. int     WINAPI StartSpoolPage(HPJOB);
  50. int     WINAPI EndSpoolPage(HPJOB);
  51. int     WINAPI WriteSpool(HPJOB, LPSTR, int);
  52. int     WINAPI CloseJob(HPJOB);
  53. int     WINAPI DeleteJob(HPJOB, int);
  54. int     WINAPI WriteDialog(HPJOB, LPSTR, int);
  55. int     WINAPI DeleteSpoolPage(HPJOB);
  56.  
  57. struct prop_item_s {
  58.     char    name[MAXSTR];
  59.     char    value[MAXSTR];
  60. };
  61.  
  62. char not_defined[] = "[Not defined]";
  63.  
  64. struct prop_item_s *
  65. get_properties(char *device)
  66. {
  67. char *entries, *p;
  68. int i, numentry;
  69. struct prop_item_s *proplist;
  70.     entries = malloc(PROFILE_SIZE);
  71.     if (entries == (char *)NULL)
  72.        return NULL;
  73.     GetPrivateProfileString(device, NULL, "", entries, PROFILE_SIZE, INIFILE);
  74.     if (strlen(entries) == 0) {
  75.         free(entries);
  76.         return NULL;
  77.     }
  78.     p = entries;
  79.     for (numentry=0; p!=(char *)NULL && strlen(p)!=0; numentry++)
  80.         p += strlen(p) + 1;
  81.     proplist = (struct prop_item_s *)malloc((numentry+1) * sizeof(struct prop_item_s));
  82.     if (proplist == (struct prop_item_s *)NULL) {
  83.         free(entries);
  84.         return NULL;
  85.     }
  86.     p = entries;
  87.     for (i=0; i<numentry; i++) {
  88.         strcpy(proplist[i].name, p);
  89.         GetPrivateProfileString(device, p, "", proplist[i].value, sizeof(proplist->value), INIFILE);
  90.         p += strlen(p) + 1;
  91.     }
  92.     proplist[numentry].name[0] = '\0';
  93.     proplist[numentry].value[0] = '\0';
  94.     free(entries);
  95.     return proplist;
  96. }
  97.  
  98. /* dialog box for selecting printer properties */
  99. BOOL CALLBACK _export
  100. PropDlgProc(HWND hDlg, UINT wmsg, WPARAM wParam, LPARAM lParam)
  101. {
  102.     char buf[128];
  103.     int iprop;
  104.     int ivalue;
  105.     WORD notify_message;
  106.     char *p;
  107.     char *value;
  108.     static char device[MAXSTR];    /* contains printer device name */
  109.     static struct prop_item_s* propitem;
  110.     char section[MAXSTR];
  111.  
  112.     switch (wmsg) {
  113.         case WM_INITDIALOG:
  114.         lstrcpy(device, (LPSTR)lParam);    /* initialise device name */
  115.         propitem = get_properties(device);
  116.         if (propitem == (struct prop_item_s *)NULL) {
  117.             EndDialog(hDlg, FALSE);
  118.             return TRUE;
  119.         }
  120.         for (iprop=0; propitem[iprop].name[0]; iprop++) {
  121.             SendDlgItemMessage(hDlg, PROP_NAME, CB_ADDSTRING, 0, 
  122.             (LPARAM)((LPSTR)propitem[iprop].name+1));
  123.         }
  124.         SendDlgItemMessage(hDlg, PROP_NAME, CB_SETCURSEL, 0, 0L);
  125.         /* force update of PROP_VALUE */
  126.         SendDlgNotification(hDlg, PROP_NAME, CBN_SELCHANGE);
  127.         return TRUE;
  128.         case WM_COMMAND:
  129.         notify_message = GetNotification(wParam,lParam);
  130.         switch (LOWORD(wParam)) {
  131.             case ID_HELP:
  132.                 SendMessage(hwndimg, help_message, 0, 0L);
  133.                 return(FALSE);
  134.             case PROP_NAME:
  135.             if (notify_message != CBN_SELCHANGE) {
  136.                 return FALSE;
  137.             }
  138.             iprop = (int)SendDlgItemMessage(hDlg, PROP_NAME, CB_GETCURSEL, 0, 0L);
  139.             if (iprop == CB_ERR) {
  140.                 return FALSE;
  141.             }
  142.             /* now look up entry in gsview.ini */
  143.             /* and update PROP_VALUE list box */
  144.             strcpy(section, device);
  145.             strcat(section, " values");
  146.             GetPrivateProfileString(section, propitem[iprop].name, "", buf, sizeof(buf)-2, INIFILE);
  147.             buf[strlen(buf)+1] = '\0';    /* put double NULL at end */
  148.                 SendDlgItemMessage(hDlg, PROP_VALUE, CB_RESETCONTENT, 0, 0L);
  149.                 SendDlgItemMessage(hDlg, PROP_VALUE, CB_ADDSTRING, 0, 
  150.                 (LPARAM)((LPSTR)not_defined));
  151.             p = buf;
  152.             if (*p != '\0') {
  153.               EnableWindow(GetDlgItem(hDlg, PROP_VALUE), TRUE);
  154.               while (*p!='\0') {
  155.                 value = p;
  156.                 while ((*p!='\0') && (*p!=','))
  157.                 p++;
  158.                 *p++ = '\0';
  159.                     SendDlgItemMessage(hDlg, PROP_VALUE, CB_ADDSTRING, 0, 
  160.                     (LPARAM)((LPSTR)value));
  161.               }
  162.             }
  163.             SendDlgItemMessage(hDlg, PROP_VALUE, CB_SELECTSTRING, -1, (LPARAM)(LPSTR)propitem[iprop].value);
  164.                 SetDlgItemText(hDlg, PROP_VALUE, propitem[iprop].value);
  165.             return FALSE;
  166.             case PROP_VALUE:
  167.             if (notify_message == CBN_SELCHANGE) {
  168.                 iprop = (int)SendDlgItemMessage(hDlg, PROP_NAME, CB_GETCURSEL, 0, 0L);
  169.                 if (iprop == CB_ERR)
  170.                     return FALSE;
  171.                 ivalue = (int)SendDlgItemMessage(hDlg, PROP_VALUE, CB_GETCURSEL, 0, 0L);
  172.                 if (ivalue == CB_ERR)
  173.                     return FALSE;
  174.                 SendDlgItemMessage(hDlg, PROP_VALUE, CB_GETLBTEXT, ivalue, (LPARAM)(LPSTR)propitem[iprop].value);
  175.             }
  176.             if (notify_message == CBN_EDITCHANGE) {
  177.                 iprop = (int)SendDlgItemMessage(hDlg, PROP_NAME, CB_GETCURSEL, 0, 0L);
  178.                 if (iprop == CB_ERR)
  179.                     return FALSE;
  180.                     GetDlgItemText(hDlg, PROP_VALUE, (LPSTR)propitem[iprop].value, sizeof(propitem->value));
  181.             }
  182.             return FALSE;
  183.             case IDOK:
  184.             for (iprop=0; propitem[iprop].name[0]; iprop++) {
  185.                 WritePrivateProfileString(device, propitem[iprop].name, propitem[iprop].value, INIFILE);
  186.             }
  187.             free((char *)propitem);
  188.             EndDialog(hDlg, TRUE);
  189.             return TRUE;
  190.             case IDCANCEL:
  191.             free((char *)propitem);
  192.             EndDialog(hDlg, FALSE);
  193.             return TRUE;
  194.         }
  195.         break;
  196.     }
  197.     return FALSE;
  198. }
  199.  
  200.  
  201. /* dialog box for selecting printer device and resolution */
  202. BOOL CALLBACK _export
  203. DeviceDlgProc(HWND hDlg, UINT wmsg, WPARAM wParam, LPARAM lParam)
  204. {
  205.     char buf[128];
  206.     int idevice;
  207.     WORD notify_message;
  208.     char *p;
  209.     char *res;
  210.     int numentry;
  211.     char entry[MAXSTR];
  212.     struct prop_item_s *proplist;
  213.  
  214.     switch (wmsg) {
  215.         case WM_INITDIALOG:
  216.         p = get_devices();
  217.         res = p;    /* save for free() */
  218.         for (numentry=0; p!=(char *)NULL && strlen(p)!=0; numentry++) {
  219.             SendDlgItemMessage(hDlg, DEVICE_NAME, CB_ADDSTRING, 0, 
  220.             (LPARAM)((LPSTR)p));
  221.             p += strlen(p) + 1;
  222.         }
  223.         free(res);
  224.         if (SendDlgItemMessage(hDlg, DEVICE_NAME, CB_SELECTSTRING, 0, (LPARAM)(LPSTR)device_name)
  225.             == CB_ERR)
  226.             SendDlgItemMessage(hDlg, DEVICE_NAME, CB_SETCURSEL, 0, 0L);
  227.         /* force update of DEVICE_RES */
  228.         SendDlgNotification(hDlg, DEVICE_NAME, CBN_SELCHANGE);
  229.         if (SendDlgItemMessage(hDlg, DEVICE_RES, CB_SELECTSTRING, 0, (LPARAM)(LPSTR)device_resolution)
  230.             == CB_ERR)
  231.             SendDlgItemMessage(hDlg, DEVICE_RES, CB_SETCURSEL, 0, 0L);
  232.         return TRUE;
  233.         case WM_COMMAND:
  234.         notify_message = GetNotification(wParam,lParam);
  235.         switch (LOWORD(wParam)) {
  236.             case ID_HELP:
  237.                 SendMessage(hwndimg, help_message, 0, 0L);
  238.                 return(FALSE);
  239.             case DEVICE_NAME:
  240.             if (notify_message != CBN_SELCHANGE) {
  241.                 return FALSE;
  242.             }
  243.             idevice = (int)SendDlgItemMessage(hDlg, DEVICE_NAME, CB_GETCURSEL, 0, 0L);
  244.             if (idevice == CB_ERR) {
  245.                 return FALSE;
  246.             }
  247.             SendDlgItemMessage(hDlg, DEVICE_NAME, CB_GETLBTEXT, idevice, (LPARAM)(LPSTR)entry);
  248.             if ( (proplist = get_properties(entry)) != (struct prop_item_s *)NULL ) {
  249.                     f